package itbour.onetouchshow.activity.version1_6;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.OneToucDialog;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.config.PictureSelectionConfig;
import com.ys.uilibrary.swip.SwipeMenuLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;

import butterknife.BindView;
import butterknife.ButterKnife;
import itbour.onetouchshow.R;
import itbour.onetouchshow.bean.ShowImageBean;
import itbour.onetouchshow.bean.canvas.CompoundPathsBean;
import itbour.onetouchshow.bean.canvas.PosBean;
import itbour.onetouchshow.bean.inching.noppt.InchingCoreRootNoPPTV10Bean;
import itbour.onetouchshow.bean.inching.noppt.InchingRelationsV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootImageValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootTextValueV10Bean;
import itbour.onetouchshow.bean.inching.noppt.NoPPTRootVideoValueV10Bean;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.constants.DataCreater;
import itbour.onetouchshow.listener.EdittextListener;
import itbour.onetouchshow.listener.SoftKeyBoardListener;
import itbour.onetouchshow.singlecase.InchingDataSingleCase;
import itbour.onetouchshow.utils.ImageLoader;
import itbour.onetouchshow.utils.L_;
import itbour.onetouchshow.utils.ShieldEmoji;
import itbour.onetouchshow.utils.UIUtils;
import itbour.onetouchshow.utils.video.EditPlayVideoFrames;
import itbour.onetouchshow.widget.BoundView;
import itbour.onetouchshow.widget.ClearEditTextView;
import itbour.onetouchshow.widget.inching.CompoundPathImageView;

/**
 * Created by zjl_d on 2017/12/5.
 */

public class VideoEditAdapterV1_6 extends RecyclerView.Adapter {
    Map<Integer, List<InchingCoreRootNoPPTV10Bean>> data;
    MakeActivityV1_6 context;
    TextView mTvChange;
    LinearLayout mBtnDelete;
    ImageView mRenameIg;
    ImageView mVideoPaly;
    ClearEditTextView mEditTextViews;
    TextView mTvCount;
    OnScrollToListener onScrollToListener;
    String[] mnmberdata = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10"};
    private ImageView nowClickImg;
    private NoPPTRootImageValueV10Bean nowImgRootValue;
    private NoPPTRootVideoValueV10Bean rootVideoValueV10Bean;
    private CompoundPathImageView nowImgMask;
    private List<SwipeMenuLayout> mSwipeMenuLayout = new ArrayList<>();
    private List<InchingCoreRootNoPPTV10Bean> listData;
    private int mpostion;
    //点击图片的接口，可返回该图片携带的数据，可从editValueBean中获取截图的路径，根据路径绘制截图形状
    private OnPictureIvClickListener onPictureIvClickListener;

    public VideoEditAdapterV1_6(MakeActivityV1_6 context, Map<Integer, List<InchingCoreRootNoPPTV10Bean>> wantData) {
        this.context = context;
        this.data = wantData;
        this.listData = data.get(0);
    }

    public CompoundPathImageView getNowImgMask() {
        return nowImgMask;
    }

    public ImageView getNowClickImg() {
        return nowClickImg;
    }

    public NoPPTRootImageValueV10Bean getNowImgRootValue() {
        return nowImgRootValue;
    }

    public NoPPTRootVideoValueV10Bean getRootVideoValueV10Bean() {
        return rootVideoValueV10Bean;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_video_edit, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    String getMainDarinNumberArra(int index) {
        return mnmberdata[index];
    }

    public void videoNotifyItemChanged(boolean isVideo) {
        if (isVideo) {
            mTvChange.setText("更换视频");
            if (mVideoPaly != null) {
                mVideoPaly.setVisibility(View.VISIBLE);
            }
        } else {
            mTvChange.setText("替换图片");
        }
        mBtnDelete.setVisibility(View.VISIBLE);
        mRenameIg.setImageResource(R.mipmap.icon_make_chage);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        TextView title = holder.title;
        LinearLayout root = holder.root;

        if (data.size() > 1) {
            title.setVisibility(View.VISIBLE);
        } else {
            title.setVisibility(View.GONE);
        }
        List<InchingCoreRootNoPPTV10Bean> listData = data.get(position);
        title.setText("镜头" + /*getMainDarinNumberArra(position)*/ (position + 1));
        final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(UIUtils.dp2px(5), UIUtils.dp2px(5), UIUtils.dp2px(5), 0);
        int i = 1;
        for (InchingCoreRootNoPPTV10Bean data : listData) {
            Integer type = data.getType();
            if (type == AppConst.ROOT_VALUE_TEXT) {
                View textView = getTextView(data, i);
                root.addView(textView, lp);
            } else if (type == AppConst.ROOT_VALUE_IMG) {
                View imageView = getImageView(data, i);
                root.addView(imageView, lp);
            } else if (type == AppConst.ROOT_VALUE_VIDEO) {
                View imageView = getVideoView(data, i);
                root.addView(imageView, lp);
            }
            i++;
        }


        if (position == data.size() - 1) {
            TextView textView = new TextView(context);
            final LinearLayout.LayoutParams lpStub =
                    new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dp2px(65));
            root.addView(textView, lpStub);
        }

    }

    private void initKeyBroadListener(EditText editText) {
        SoftKeyBoardListener.setListener(context, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
            @Override
            public void keyBoardShow(int height) {
                //  L_.i("键盘显示 高度" + height);

//                LinearLayoutManager manager = (LinearLayoutManager) this.getLayoutManager();
//                // 当不滚动时FirstVisibleItemPosition();
//                int lastPosition = manager.findLastVisibleItemPosition();

//                int firstPostion = manager.find
            }

            @Override
            public void keyBoardHide(int height) {
                //L_.i("键盘隐藏 高度" + height);
                editText.clearFocus();
            }
        });
    }


    public void startPhotoActivity(ImageView imageView, String imageUrl, CompoundPathsBean compoundPathsBean, PosBean posBean) {

        //确定当前选中的位置
        int centerWidth = UIUtils.WHD()[0];
        int pageWidth = context.noPptCoreData.getPageWidth();
        int pageHeight = context.noPptCoreData.getPageHeight();
        float scaleWH = pageWidth * 1.0f / pageHeight;
        // float scaleW = centerWidth * 1.0f / pageWidth;
        int centerHeight = (int) (centerWidth / scaleWH);


        int location[] = new int[2];
        imageView.getLocationOnScreen(location);
//        intent.putExtra("left", location[0]);
//        intent.putExtra("top", location[1]);
//        intent.putExtra("height", imageView.getHeight());
//        intent.putExtra("width", imageView.getWidth());
//        intent.putExtra("imageUrl", imagUrl);
        ShowImageBean showImageBean = new ShowImageBean();
        showImageBean.setW(centerWidth);
        showImageBean.setH(centerHeight);
        showImageBean.setPageW(pageWidth);
        showImageBean.setPageH(pageHeight);
        showImageBean.setX(location[0]);
        showImageBean.setY(location[1]);
        showImageBean.setImageUrl(imageUrl);
        Intent intent = DragPhotoActivity.getLaunchIntent(context, showImageBean, compoundPathsBean, posBean);
        //Intent intent = new Intent(context, DragPhotoActivity.class);
        context.startActivity(intent);
        if (context instanceof MakeActivityV1_6) {
            context.overridePendingTransition(0, 0);
        }

        //overridePendingTransition(0,0);
    }

    private View getImageView(InchingCoreRootNoPPTV10Bean data, int position) {

        final View inflate = View.inflate(context, R.layout.video_adapter_img_type, null);
        SwipeMenuLayout swipeMenuLayout = inflate.findViewById(R.id.id_root_swp);
        mSwipeMenuLayout.add(swipeMenuLayout);
        // setVideoBound(data);
        LinearLayout btnDelete = inflate.findViewById(R.id.btnDelete_ly);
        LinearLayout btnReName = inflate.findViewById(R.id.btnReName_ly);
        ImageView edit_img_hint = inflate.findViewById(R.id.edit_img_hint);
        RelativeLayout boundRoot = inflate.findViewById(R.id.id_bound_rot);

        TextView tvTip = inflate.findViewById(R.id.tv_img_tip);
        TextView tvImageChange = inflate.findViewById(R.id.id_tv_chage);
        ImageView renameIg = inflate.findViewById(R.id.id_icon_rename);
        TextView tvDel = inflate.findViewById(R.id.id_tv_del);
        TextView title = inflate.findViewById(R.id.tv_video_img_title);
        String topic = data.getTopic();
        title.setText(topic);
        ImageView imgChange = inflate.findViewById(R.id.iv_video_edit_img);
        CompoundPathImageView mask = inflate.findViewById(R.id.img_mask);
        RelativeLayout ryEdit = inflate.findViewById(R.id.iv_ry_edit);

        TextView tvPageCount = inflate.findViewById(R.id.id_page_count);

        NoPPTRootImageValueV10Bean value = (NoPPTRootImageValueV10Bean) data.getValue();
        CompoundPathsBean compoundPathsBean = value.getMask();
        mask.setData(compoundPathsBean, UIUtils.dp2px(80), UIUtils.dp2px(80));
        mask.setColorString("#252525");
        tvTip.setText("(点击替换图片)");
        String thumbs = value.getValue();
        PosBean pos = value.getPos();
        ImageLoader.getInstance().showImage(context,
                ImageLoader.getShrinkImageUrl(thumbs, UIUtils.dip2px(80), UIUtils.dip2px(80)), imgChange);

        //L_.e("缩略图："+thumbs);

        List<InchingRelationsV10Bean> relations = data.getRelations();
        String bgThumb = relations.get(0).bgThumb;
        tvImageChange.setText("替换图片");
        tvDel.setText("删除图片");
        // edit_img_hint.setImageResource(bgThumb);
        ImageLoader.getInstance().showCircleImageWithBorderCenterCrop(context, bgThumb, edit_img_hint);
        if (TextUtils.isEmpty(thumbs)) {
            btnDelete.setVisibility(View.GONE);
            tvImageChange.setText("添加图片");
            renameIg.setImageResource(R.mipmap.icon_make_add);
        }

        if (position <= 9) {
            tvPageCount.setText(getMainDarinNumberArra(position - 1));
        } else {
            tvPageCount.setText(position + "");
        }

        showBoundView(data, boundRoot, edit_img_hint);
        //showBoundView(data, boundRoot);
        swipeMenuLayout.setOnSmoothExpandStaus(new SwipeMenuLayout.OnSmoothExpandStausListener() {
            @Override
            public void isExpand(boolean isExpand) {
                if (onPictureIvClickListener != null) {
                    if (isExpand) {
                        cleanEditTextHasFous();
                    }
                }

            }
        });

        ryEdit.setOnClickListener(v -> {
            // swipeMenuLayout.quickClose();
            swipeMenuLayout.smoothExpand();
            nowClickImg = imgChange;
            nowImgRootValue = value;
            nowImgMask = mask;
            mTvChange = tvImageChange;
            mBtnDelete = btnDelete;
            mRenameIg = renameIg;
            if (onPictureIvClickListener != null) {
                onPictureIvClickListener.addBoundView(data);
            }

        });


        boundRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startShowPhotoActivity(bgThumb, data.getRelations().get(0).getBound(), data.getRelations().get(0).getPos());

                // ShowImageBean showImageBean

                //startPhotoActivity(edit_img_hint,bgThumb,data.getRelations().get(0).getBound(),data.getRelations().get(0).getPos());
            }
        });

        btnReName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                swipeMenuLayout.quickClose();
                nowClickImg = imgChange;
                nowImgRootValue = value;
                nowImgMask = mask;
                mTvChange = tvImageChange;
                mBtnDelete = btnDelete;
                mRenameIg = renameIg;
                gotoSelectPhotoType();
            }
        });

        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cleanEditTextHasFous();
                swipeMenuLayout.quickClose();
                value.setValue("");
                //ImageLoader.getInstance().showImage(context, "", img);
                imgChange.setImageResource(R.mipmap.icon_image_place);
                btnDelete.setVisibility(View.GONE);
                tvImageChange.setText("添加图片");
                renameIg.setImageResource(R.mipmap.icon_make_add);
            }
        });

//        edit_img_hint.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                startShowPhotoActivity(bgThumb, data.getRelations().get(0).getBound(), data.getRelations().get(0).getPos());
//            }
//        });

        return inflate;
    }


    private void gotoSelectPhotoType() {
        int chooseMode = PictureMimeType.ofImage();
        // 进入相册 以下是例子：不需要的api可以不写
        PictureSelector.create(context)
                .openGallery(chooseMode)// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style
                .maxSelectNum(1)// 最大图片选择数量
                .minSelectNum(1)// 最小选择数量
                .imageSpanCount(4)// 每行显示个数
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                .isCamera(false)// 是否显示拍照按钮
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .enableCrop(false)// 是否裁剪
                .compress(true)// 是否压缩
                .synOrAsy(true)//同步true或异步false 压缩 默认同步
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                .isGif(false)// 是否显示gif图片
                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                .circleDimmedLayer(false)// 是否圆形裁剪
                .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .openClickSound(false)// 是否开启点击声音
                //.selectionMedia(false)// 是否传入已选图片
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(AppConst.TOPHOTO);//结果回调onActivityResult code
    }


    public void romvewBoundView(RelativeLayout boundRoot) {
        if (boundRoot != null) {
            for (int i = 0; i < boundRoot.getChildCount(); i++) {
                View viewById = boundRoot.findViewById(R.id.video_edit_bound);
                if (viewById != null) {
                    viewById.clearAnimation();
                    boundRoot.removeView(viewById);
                }
            }
        }
    }

    public void showBoundView(InchingCoreRootNoPPTV10Bean data, RelativeLayout rootView, ImageView imageView) {
        if (context.noPptCoreData == null) {
            return;
        }
        //确定当前选中的位置
        // int centerWidth = UIUtils.dip2px(150);
        int centerHeight = UIUtils.dip2px(80);
        int pageWidth = context.noPptCoreData.getPageWidth();
        int pageHeight = context.noPptCoreData.getPageHeight();

        float scaleWH = pageWidth * 1.0f / pageHeight;
        // int centerWidth = UIUtils.dip2px(150);
        int centerWidth = (int) (centerHeight * scaleWH);


        float scaleW = centerWidth * 1.0f / pageWidth;
        float scaleH = centerHeight * 1.0f / pageHeight;

        CompoundPathsBean bound = data.getRelations().get(0).getBound();
        PosBean pos = data.getRelations().get(0).getPos();
        float w = bound.getW() * scaleW;
        float h = bound.getH() * scaleH;
        float x = pos.getX() * scaleW;
        float y = pos.getY() * scaleH;
        BoundView pathsView = new BoundView(context);
        pathsView.setId(R.id.video_edit_bound);
        pathsView.setData(bound, (int) w, (int) h);

        ViewGroup.LayoutParams layoutParams = rootView.getLayoutParams();
        layoutParams.width = centerWidth;
        layoutParams.height = centerHeight;
        rootView.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams((int) w, (int) h);
        lp.leftMargin = (int) x;
        lp.topMargin = (int) y;
        rootView.addView(pathsView, lp);
        L_.i("addBoundView===" + centerWidth + "centerHeight===" + centerHeight);
    }


    public void startShowPhotoActivity(String imageUrl, CompoundPathsBean bound, PosBean posBean) {
        //确定当前选中的位置
        int centerWidth = UIUtils.WHD()[0];
        int pageWidth = context.noPptCoreData.getPageWidth();
        int pageHeight = context.noPptCoreData.getPageHeight();
        float scaleWH = pageWidth * 1.0f / pageHeight;
        // float scaleW = centerWidth * 1.0f / pageWidth;
        int centerHeight = (int) (centerWidth / scaleWH);
        Intent intent = ShowPhotoActivity.getLaunchIntent(context, imageUrl, centerWidth, centerHeight, pageWidth, pageHeight, bound, posBean);
        context.startActivity(intent);
    }


    private View getVideoView(InchingCoreRootNoPPTV10Bean data, int position) {

        final View inflate = View.inflate(context, R.layout.video_adapter_img_type, null);
        RelativeLayout ryEdit = inflate.findViewById(R.id.iv_ry_edit);
        SwipeMenuLayout swipeMenuLayout = inflate.findViewById(R.id.id_root_swp);
        mSwipeMenuLayout.add(swipeMenuLayout);
        LinearLayout btnDelete = inflate.findViewById(R.id.btnDelete_ly);
        LinearLayout changeVideo = inflate.findViewById(R.id.btnReName_ly);
        TextView title = inflate.findViewById(R.id.tv_video_img_title);
        TextView tvChange = inflate.findViewById(R.id.id_tv_chage);
        ImageView renameIg = inflate.findViewById(R.id.id_icon_rename);
        ImageView videoPaly = inflate.findViewById(R.id.iv_video_paly);
        ImageView edit_img_hint = inflate.findViewById(R.id.edit_img_hint);
        RelativeLayout boundRoot = inflate.findViewById(R.id.id_bound_rot);
        videoPaly.setVisibility(View.VISIBLE);
        TextView tvTip = inflate.findViewById(R.id.tv_img_tip);
        String topic = data.getTopic();
        title.setText(topic);
        ImageView img = inflate.findViewById(R.id.iv_video_edit_img);
        CompoundPathImageView mask = inflate.findViewById(R.id.img_mask);
        TextView tvDel = inflate.findViewById(R.id.id_tv_del);
        TextView tvPageCount = inflate.findViewById(R.id.id_page_count);
        //  setVideoBound(data);
        tvTip.setText("(点击替换视频)");
        //读取视频数据 需要截取的时间 Relations
        List<InchingRelationsV10Bean> relations = data.getRelations();
        InchingRelationsV10Bean inchingRelationsV10Bean = relations.get(0);
        String bgThumb = relations.get(0).bgThumb;
        /*修改Value*/
        NoPPTRootVideoValueV10Bean value = (NoPPTRootVideoValueV10Bean) data.getValue();
        //CompoundPathsBean compoundPathsBean = value.getMask();
        //  mask.setData(compoundPathsBean, UIUtils.dp2px(80), UIUtils.dp2px(80));
        // mask.setColorString("#252525");

        ImageLoader.getInstance().showCircleImageWithBorderCenterCrop(context, bgThumb, edit_img_hint);
        showBoundView(data, boundRoot, edit_img_hint);

        swipeMenuLayout.setOnSmoothExpandStaus(new SwipeMenuLayout.OnSmoothExpandStausListener() {
            @Override
            public void isExpand(boolean isExpand) {
                if (onPictureIvClickListener != null) {
                    if (isExpand) {
                        cleanEditTextHasFous();
                    }
                }
            }
        });

        if (position <= 9) {
            tvPageCount.setText(getMainDarinNumberArra(position - 1));
        } else {
            tvPageCount.setText(position + "");
        }


        String thumbs = value.getValue();
        if (TextUtils.isEmpty(thumbs)) {
            btnDelete.setVisibility(View.GONE);
            tvChange.setText("添加视频");
            renameIg.setImageResource(R.mipmap.icon_make_add);
        }
        ImageLoader.getInstance().showImage(context, thumbs, img);
        tvChange.setText("替换视频");
        tvDel.setText("删除视频");
        ryEdit.setOnClickListener(v -> {
            swipeMenuLayout.smoothExpand();
            nowClickImg = img;
            rootVideoValueV10Bean = value;
            mTvChange = tvChange;
            mBtnDelete = btnDelete;
            mRenameIg = renameIg;
            InchingDataSingleCase.INSTANCE.setDuration((long) (inchingRelationsV10Bean.getDuration() * 1000L));
        });

        boundRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startShowPhotoActivity(bgThumb, data.getRelations().get(0).getBound(), data.getRelations().get(0).getPos());
            }
        });
        changeVideo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nowClickImg = img;
                rootVideoValueV10Bean = value;
                mTvChange = tvChange;
                mBtnDelete = btnDelete;
                mRenameIg = renameIg;
                InchingDataSingleCase.INSTANCE.setDuration((long) (inchingRelationsV10Bean.getDuration() * 1000L));
                gotoSelectType();
                swipeMenuLayout.quickClose();

            }
        });

        btnDelete.setOnClickListener(v -> {
            cleanEditTextHasFous();
            swipeMenuLayout.quickClose();
            value.setValue("");
            value.setUrl("");
            videoPaly.setVisibility(View.GONE);
            img.setImageResource(R.mipmap.icon_video_place);
            // ImageLoader.getInstance().showImage(context, "", img);
            btnDelete.setVisibility(View.GONE);
            tvChange.setText("添加视频");
            mVideoPaly = videoPaly;
            renameIg.setImageResource(R.mipmap.icon_make_add);
            UIUtils.getHandler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (onPictureIvClickListener != null) {
                        onPictureIvClickListener.removeBoundView();
                    }

                }
            }, 100);
        });

        return inflate;
    }

    private void gotoSelectType() {
        int chooseMode = PictureMimeType.ofVideo();
        // 进入相册 以下是例子：不需要的api可以不写
        PictureSelector.create(context)
                .openGallery(chooseMode)// 全部.PictureMimeType.ofAll()、图片.ofImage()、视频.ofVideo()、音频.ofAudio()
                .theme(R.style.picture_default_style)// 主题样式设置 具体参考 values/styles   用法：R.style.picture.white.style
                .maxSelectNum(1)// 最大图片选择数量
                .setOnSelcterVideoFilter(new PictureSelectionConfig.OnSelectClickListner() {

                    @Override
                    public boolean ishasFilter(String url, Activity activity) {
                        if (url != null) {
                            if (EditPlayVideoFrames.getVideoEncodeWithPath(url)) {
                                String duration = UIUtils.videoWHDA(url)[2];
                                //int durationCount = Integer.valueOf(duration);
                                int durationCount = (int) Double.parseDouble(duration);
                                if (durationCount < 1) {
                                    showUpdateWorkDialog(activity, false, true, false);
                                    return false;
                                }
                                if (durationCount > 180) {
                                    showUpdateWorkDialog(activity, false, false, true);
                                    return false;
                                }
                                return true;
                            } else {
                                showUpdateWorkDialog(activity, true, false, false);
                                return false;
                            }
                            // L_.e(videoEncodeWithPath);
                        }
                        return false;
                    }
                })
                .minSelectNum(1)// 最小选择数量
                .imageSpanCount(4)// 每行显示个数
                .selectionMode(PictureConfig.SINGLE)// 多选 or 单选
                .isCamera(false)// 是否显示拍照按钮
                .isZoomAnim(true)// 图片列表点击 缩放效果 默认true
                .enableCrop(false)// 是否裁剪
                .compress(true)// 是否压缩
                .synOrAsy(true)//同步true或异步false 压缩 默认同步
                .glideOverride(160, 160)// glide 加载宽高，越小图片列表越流畅，但会影响列表图片浏览的清晰度
                .hideBottomControls(false)// 是否显示uCrop工具栏，默认不显示
                .isGif(false)// 是否显示gif图片
                .freeStyleCropEnabled(true)// 裁剪框是否可拖拽
                .circleDimmedLayer(false)// 是否圆形裁剪
                .showCropFrame(true)// 是否显示裁剪矩形边框 圆形裁剪时建议设为false
                .showCropGrid(false)// 是否显示裁剪矩形网格 圆形裁剪时建议设为false
                .openClickSound(false)// 是否开启点击声音
                //.selectionMedia(false)// 是否传入已选图片
                .minimumCompressSize(100)// 小于100kb的图片不压缩
                .forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code

    }

    @SuppressLint("ValidFragment")
    public void showUpdateWorkDialog(Activity activity, boolean type, boolean isShort, boolean isLong) {
        String contexttext = null;
        if (type) {
            // contexttext = "无法获取视频信息, 请重新选择";
            contexttext = DataCreater.videoTypes;
        }
        if (isShort) {
            contexttext = DataCreater.videoShort;
        }
        if (isLong) {
            contexttext = DataCreater.videoLong;
        }
        String finalContexttext = contexttext;


        new OneToucDialog.Builder(activity)
                .content(finalContexttext)
                .positiveText("确定").onPositive(new OneToucDialog.SingleButtonCallback() {
            @Override
            public void onClick(OneToucDialog dialog, DialogAction which) {
            }
        }).setType(OneToucDialog.Builder.ONE_TOUCH_TYPE)
                .cancelable(false)
                .show();
    }

    public void cleanEditTextHasFous() {
        if (mEditTextViews != null) {
            UIUtils.hideKeyboard(mEditTextViews);
            //initKeyBroadListener(mEditTextViews);
            mEditTextViews.setClearIcon(false);
        }
        if (mTvCount != null) {
            mTvCount.setVisibility(View.INVISIBLE);
        }


    }

    public int getMpostion() {
        return mpostion;
    }

    public void setMpostion(int postion) {
        this.mpostion = postion;
        L_.e("当前选择的行数" + mpostion);
    }

    public ClearEditTextView getmEditTextViews() {
        return mEditTextViews;
    }

    private View getTextView(InchingCoreRootNoPPTV10Bean data, int position) {

        //  L_.e(position + "getTextView  position ");
        final View inflate = View.inflate(context, R.layout.video_adapter_text_type, null);
        TextView title = inflate.findViewById(R.id.tv_video_text_title);
        ClearEditTextView editTextView = inflate.findViewById(R.id.et_text_valuel);
        // mEditTextViews.add(editTextView);
        TextView tvCount = inflate.findViewById(R.id.tv_text_count);
        ImageView imageClean = inflate.findViewById(R.id.id_clean_all);

        ImageView edit_img_hint = inflate.findViewById(R.id.edit_img_hint);
        RelativeLayout boundRoot = inflate.findViewById(R.id.id_bound_rot);


        TextView tvPageCount = inflate.findViewById(R.id.id_page_count);

        String topic = data.getTopic();
        title.setText(topic);

        if (position <= 9) {
            tvPageCount.setText(getMainDarinNumberArra(position - 1));
        } else {
            tvPageCount.setText(position + "");
        }

        NoPPTRootTextValueV10Bean value = (NoPPTRootTextValueV10Bean) data.getValue();
        List<InchingRelationsV10Bean> relations = data.getRelations();
        String bgThumb = relations.get(0).bgThumb;
        // InchingRelationsV10Bean inchingRelationsV10Bean = relations.get(0);
        // String bgThumb = relations.get(0).bgThumb;
        ImageLoader.getInstance().showCircleImageWithBorderCenterCrop(context, bgThumb, edit_img_hint);
        /*修改Value*/
        String tip = value.getValue();
        //editTextView.setHint(tip);
        editTextView.setText(tip);
        // tvCount.setText(value.getMax() + "");
        editTextView.setFilters(
                new InputFilter[]{/*new InputFilter.LengthFilter(value.getMax()), */new InputFilter() {
                    @Override
                    public CharSequence filter(CharSequence charSequence, int i, int i1, Spanned spanned, int i2, int i3) {
                        Matcher matcher = ShieldEmoji.isEmoji().matcher(charSequence);
                        if (matcher.find()) {
                            return "";
                        }
                        return null;
                    }
                }});
        if (editTextView.getText().length() > 0) {
            tvCount.setText(editTextView.getText().length() + "/" + value.getMax() + "");
            tvCount.setVisibility(View.INVISIBLE);
            imageClean.setVisibility(View.INVISIBLE);
            editTextView.setClearIcon(false);
        }
        initKeyBroadListener(editTextView);
        showBoundView(data, boundRoot, edit_img_hint);
        editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                L_.i("getTextView===" + hasFocus);
                if (hasFocus) {
                    setMpostion(position);
                    mEditTextViews = editTextView;
                    mTvCount = tvCount;
                    for (int i = 0; i < mSwipeMenuLayout.size(); i++) {
                        mSwipeMenuLayout.get(i).quickClose();
                    }
                    editTextView.setClearIcon(true);
                    tvCount.setText(editTextView.getText().length() + "/" + value.getMax() + "");
                    if (editTextView.getText().length() == 0) {
                        imageClean.setVisibility(View.GONE);
                    } else {
                        imageClean.setVisibility(View.VISIBLE);
                    }
                    tvCount.setVisibility(View.VISIBLE);

                } else {
                    editTextView.setClearIcon(false);
                    tvCount.setVisibility(View.GONE);
                    imageClean.setVisibility(View.GONE);
                    // thumbs.addAll(noPptCoreData.getSceneThumbs());
                }
            }
        });


        boundRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startShowPhotoActivity(bgThumb, data.getRelations().get(0).getBound(), data.getRelations().get(0).getPos());
            }
        });

        editTextView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                if (onScrollToListener != null) {
                    onScrollToListener.scrollTo(editTextView, position);
                }
            }

        });
//


        imageClean.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editTextView.setText("");
                //tvCount.setText(value.getMax() + "/" + value.getMax() + "");
                imageClean.setVisibility(View.GONE);
            }
        });

        //限制输入
        EdittextListener edittextListener = new EdittextListener(editTextView, value.getMax()) {
            @Override
            public void afterTextChanged(Editable editable) {
                int end = editable.length();
                UnderlineSpan[] da =
                        editable.getSpans(0, end, UnderlineSpan.class);
                if (da.length > 0) {
                } else {
                    if (editable.length() > value.getMax()) {
                        tvCount.setText(value.getMax() + "/" + value.getMax() + "");
                        editTextView.setText(editable.subSequence(0, value.getMax()));
                        editTextView.setSelection(editTextView.getText().length());
                    } else {
                        tvCount.setText(editable.length() + "/" + value.getMax() + "");
                    }

                }
                if (editable.length() > 0) {
                    imageClean.setVisibility(View.VISIBLE);
                    String afterString = editable.toString();
                    value.setValue(afterString);
                } else {
                    //tvCount.setText("");
                    tvCount.setText(0 + "/" + value.getMax() + "");
                    value.setValue("");
                }


            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                super.beforeTextChanged(s, start, count, after);
                String afterString = s.toString();
                value.setValue(afterString);
            }
        };
        editTextView.addTextChangedListener(edittextListener);
        return inflate;
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public void setOnPictureIvClickListener(OnPictureIvClickListener onPictureIvClickListener) {
        this.onPictureIvClickListener = onPictureIvClickListener;
    }

    public void setOnScrollToListener(OnScrollToListener onScrollToListener) {
        this.onScrollToListener = onScrollToListener;
    }


    public interface OnPictureIvClickListener {
        void getPictureIv();


        void addBoundView(InchingCoreRootNoPPTV10Bean data);

        void removeBoundView();


        void getCurrentPic(int position, String url);
    }

    public interface OnScrollToListener {
        void scrollTo(EditText editText, int postion);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_video_title)
        TextView title;
        @BindView(R.id.ll_video_root)
        LinearLayout root;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
